//
//  TSSendMsgTableView.m
//  chart
//
//  Created by xu on 15/7/29.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSSendMsgTableView.h"
#import "TSMessageListCell.h"
#import "TSRecieverListModel.h"
#import "TSCreateMsgViewController.h"
#import "TSMsgStatisticsViewController.h"

@implementation TSSendMsgTableView{
    TSReceiveMsgListModel *sendListModel;
    NSIndexPath *currentIndexPath;
}

@synthesize dataArray;

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self=[super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        
        self.dataSource = self;
        self.delegate = self;
        
        sendListModel = [[TSReceiveMsgListModel alloc] init];
        
        [self.header beginRefreshing];
    }
    
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *cell_head_identify = @"cellIdentify_head";
        TSMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_head_identify];
        if (!cell) {
            cell = [[TSMessageListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_head_identify isHeader:true];
        }
        return cell;
    }
    
    static NSString *cell_identify = @"cellIdentify";
    TSMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
    if (!cell) {
        cell = [[TSMessageListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
    }
    
    if (dataArray) {
        TSReceiveMsgModel *model = [dataArray objectAtIndex:indexPath.row-1];
        cell.isSendNotReceive = YES;
        [cell setModel:model];
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray) {
        return [dataArray count] + 1;
    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return;
    }
    
    currentIndexPath = indexPath;
    UIViewController *vc = [self getViewController];
    if (vc) {
        UIActionSheet *action_sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"修改", @"查看", nil];
        [action_sheet showInView:[self getViewController].view];
    }
}


#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self showMsgEditViewController];
    }
    else if (buttonIndex == 1){
        [self showMsgScanViewController];
    }
}

- (void)showMsgEditViewController
{
    TSCreateMsgViewController *resend_vc = [[TSCreateMsgViewController alloc] init];
    [resend_vc setHidesBottomBarWhenPushed:YES];
    
    if (dataArray) {
        TSReceiveMsgModel *model = [dataArray objectAtIndex:currentIndexPath.row-1];
        resend_vc.msgId = model.msgId;
    }
    
    [[self getViewController].navigationController pushViewController:resend_vc animated:YES];
}

- (void)showMsgScanViewController
{
    TSMsgStatisticsViewController *statistics_vc = [[TSMsgStatisticsViewController alloc] init];
    
    if (dataArray && dataArray.count > currentIndexPath.row) {
        TSReceiveMsgModel *model = [dataArray objectAtIndex:currentIndexPath.row];
        statistics_vc.msgId = model.msgId;
    }
    
    [[self getViewController].navigationController pushViewController:statistics_vc animated:YES];
}

#pragma mark - requestData

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    sendListModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [sendListModel fetchWithPath:kRequestUrl_SendMessage_outSend_message completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (sendListModel.list.count > 0) {
            strongSelf.footer.hidden = NO;
            strongSelf->dataArray = sendListModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

- (void)footerRequestData
{
    WS(weakSelf);
    self.pageIndex++;
    sendListModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [sendListModel fetchWithPath:kRequestUrl_SendMessage_outSend_message completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        
        if (sendListModel.list.count == 0) {
            strongSelf.footer.hidden = YES;
        }
        else{
            strongSelf->dataArray = sendListModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}


@end
